package com.bjdd.jdy.ui.previewImg;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bjdd.jdy.R;
import com.bjdd.jdy.base.RxManager;
import com.bjdd.jdy.util.ImageUtil;
import com.bjdd.jdy.view.widget.PinchImageView;
import com.bjdd.jdy.view.widget.ZoomImage;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

public class PreviewImgActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
    //@Bind(R.id.img_zoom)
    private ImageView imageView;
    private TextView tvIndex;
    private Button btnDel;
    private ViewPager viewPager;
    private List<View> viewList;
    private List<String> paths;
    private ImageUtil imageUtil;
    private int mode;
    private int index = 0;
    private int count;
    private String eventName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preview_img);
        init();
    }
    private void init(){
//        imageView = (ImageView)findViewById(R.id.img_zoom);
//        ImageUtil imageUtil = new ImageUtil(this);
//        imageUtil.loadUrlImage("http://b.zol-img.com.cn/sjbizhi/images/2/320x510/1352891767829.jpg",imageView);
        //imageView.setImageResource(R.mipmap.guide1);
        imageUtil = new ImageUtil(this);
        Bundle bundle = getIntent().getExtras();
        mode = bundle.getInt("mode");
        index = bundle.getInt("index");
        eventName = bundle.getString("eventName");
        ArrayList list = bundle.getParcelableArrayList("data");
        paths = (List<String>) list.get(0);
        count = paths.size();
        viewPager = (ViewPager)findViewById(R.id.viewpage);
        tvIndex = (TextView)findViewById(R.id.tv_viewpage_index);
        btnDel = (Button)findViewById(R.id.btn_del);
        viewList = new ArrayList<>();
        LayoutInflater inflater = getLayoutInflater();
        for (int i=0;i<paths.size();i++){
            View view = inflater.inflate(R.layout.item_preview_img,null);
            ImageView imageView = (ImageView)view.findViewById(R.id.img_zoom);
            switch (mode){
                case ZoomImage.LOCAL_MODE:
                    imageUtil.loadLocalImage(paths.get(i),imageView);
                    break;
                case ZoomImage.URL_MODE:
                    imageUtil.loadUrlImage(paths.get(i),imageView);
                    break;
            }
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
            viewList.add(view);
        }
        PagerAdapter adapter = new PagerAdapter() {
            @Override
            public int getCount() {
                return viewList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                //super.destroyItem(container, position, object);
                container.removeView(viewList.get(position));
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                //return super.instantiateItem(container, position);
                container.addView(viewList.get(position));
                return viewList.get(position);
            }
        };
        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(this);
        viewPager.setCurrentItem(index);
        tvIndex.setText((index+1)+"/"+count);
        RxManager rxManager = new RxManager();
        btnDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rxManager.post(eventName,index);
                finish();
            }
        });
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        index = position;
        tvIndex.setText((index+1)+"/"+count);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
